package implementations.dm_kernel.host;

import implementations.dm_kernel.ConnectorImpl;
import implementations.dm_kernel.MessageControlImpl;
import implementations.dm_kernel.Server;
import implementations.sm_kernel.JCL_FacadeImpl;
import interfaces.kernel.JCL_connector;
import interfaces.kernel.JCL_facade;
import interfaces.kernel.JCL_message_control;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Properties;

import commom.GenericConsumer;
import commom.GenericResource;

public class MainHost extends Server{
	
	private JCL_facade jcl;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// Read properties file.
		Properties properties = new Properties();
		try {
		    properties.load(new FileInputStream("../jcl_conf/config.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		int hostPort = Integer.parseInt(properties.getProperty("hostPort"));
		MainHost m = new MainHost(hostPort);
	}
	
	public MainHost(int port){
		super(port);		
		jcl = JCL_FacadeImpl.getInstance();
		this.begin();
	}

	@Override
	protected void beforeListening() {
		JCL_message_control msg = new MessageControlImpl();
		msg.setType(-1);
		
		msg.setRegisterData(getNameIPPort());
		
		JCL_connector controlConnector = new ConnectorImpl();
		// Read properties file.
		Properties properties = new Properties();
		try {
		    properties.load(new FileInputStream("../jcl_conf/config.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		String serverAdd = properties.getProperty("serverMainAdd");
		int serverPort = Integer.parseInt(properties.getProperty("serverMainPort"));
		controlConnector.connect(serverAdd, serverPort);
		
		JCL_message_control msgr = controlConnector.sendReceive(msg);
		
		if(msgr.getRegisterData().length==1)	
			System.out.println("HOST JCL is OK");
		else System.err.println("HOST JCL NOT STARTED");		
		
		controlConnector.disconnect();
	}
	
	private String[] getNameIPPort(){
		try {
			 
			InetAddress ip = InetAddress.getLocalHost();
			System.out.println("Current IP address : " + ip.getHostAddress());
	 
			NetworkInterface network = NetworkInterface.getByInetAddress(ip);
	 
			byte[] mac = network.getHardwareAddress();
	 
			System.out.print("Current MAC address : ");
	 
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < mac.length; i++) {
				sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));		
			}
			
			if (sb.length()==0) sb.append(ip.getHostAddress());
			
			System.out.println(sb.toString());
			String[] result = {sb.toString(), ip.getHostAddress(), "5151"};
			
			return result;
			
	 
		} catch (Exception e) {
			
			try {
				InetAddress ip = InetAddress.getLocalHost();
			
				String sb = ip.getHostAddress();
				String[] result = {sb, ip.getHostAddress(), "5151"};
				return result;
			} catch (UnknownHostException e1) {
				System.err.println("cannot collect host address");
				return null;
			}
			
			
		}
	}

	@Override
	protected void duringListening() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected GenericConsumer<Socket> createSocketConsumer(
			GenericResource<Socket> r) {
		// TODO Auto-generated method stub
		return new SocketConsumer<Socket>(r);
	}

}
